package com.liujit.generator.controller;

import cn.hutool.system.SystemUtil;
import cn.hutool.system.UserInfo;
import com.liujit.generator.domain.model.TableInfo;
import com.liujit.generator.domain.model.TemplateInfo;
import com.liujit.generator.domain.param.GeneratorParam;
import com.liujit.generator.domain.result.GeneratorInitResult;
import com.liujit.generator.service.GeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 代码生成接口
 * @Author LiuJun
 * @Date 2020/8/14 10:39 上午
 */
@AllArgsConstructor
@RestController
@Api(tags = "代码生成接口")
@RequestMapping("/code")
public class CodeController {

    private GeneratorService generatorService;

    @ApiOperation(value = "初始化参数获取", notes = "初始化参数获取")
    @GetMapping(value = "/init")
    public GeneratorInitResult init() {
        GeneratorInitResult result = new GeneratorInitResult();
        UserInfo userInfo = SystemUtil.getUserInfo();
        result.setProjectPath(userInfo.getCurrentDir());
        result.setAuthor(userInfo.getName().replace("/", ""));
        return result;
    }

    @ApiOperation(value = "获取模板信息", notes = "根据项，获取数据库表信息")
    @GetMapping(value = "/template")
    public List<TemplateInfo> getTemplateInfo() {
        return generatorService.getTemplateInfo();
    }

    @ApiOperation(value = "获取数据库表信息", notes = "根据项目名，获取数据库表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", dataType = "String", paramType = "path", value = "项目名") })
    @GetMapping(value = "/{projectName}/tableInfo")
    public List<TableInfo> getTableInfo(@PathVariable String projectName) {
        return generatorService.getTableInfo(projectName);
    }

    @ApiOperation(value = "生成代码", notes = "根据请求参数生成代码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", dataType = "GeneratorParam", paramType = "body", value = "代码生成请求参数") })
    @PostMapping(value = "/generate")
    public void generate(@RequestBody GeneratorParam param) {
        generatorService.generate(param);
    }
}
